package com.atguigu.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrAttrgroupRelationService;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.gulimall.product.vo.AttrGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;


/**
 * 属性分组
 *
 * @author zhangjianyou
 * @email 1844897920@qq.com
 * @date 2022-06-01 12:37:09
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    ///product/attrgroup/attr/relation
    @PostMapping("/attr/relation")
    private R attrRelation(@RequestBody List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntityList){
        attrAttrgroupRelationService.saveBatch(attrAttrgroupRelationEntityList);
        return R.ok();
    }

    ///product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    private R getAttrgroupAndAttrByCatelogId(@PathVariable Long catelogId){
        List<AttrGroupVo> attrGroupVoList = attrGroupService.getAttrgroupAndAttrByCatelogId(catelogId);
        return R.ok().put("data",attrGroupVoList);
    }

    ///product/attrgroup/{attrgroupId}/noattr/relation
    @GetMapping("/{attrgroupId}/noattr/relation")
    private R noattrRelation(@PathVariable Long attrgroupId,@RequestParam Map<String, Object> params){
        PageUtils pageUtils = attrService.noattrRelation(attrgroupId,params);
        return R.ok().put("page",pageUtils);
    }
    ///product/attrgroup/attr/relation/delete
    @PostMapping("attr/relation/delete")
    public R deleteRelation(@RequestBody List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntityList){
        attrAttrgroupRelationService.deleteRelation(attrAttrgroupRelationEntityList);
        return R.ok();
    }
    /**
     * 列表
     */
    //product/attrgroup/{attrgroupId}/attr/relation
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable Long attrgroupId){
        List<AttrEntity> attrEntityList = attrService.attrEntityList(attrgroupId);
        return R.ok().put("data",attrEntityList);
    }
    @RequestMapping("/list/{catId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Integer catId){

        //PageUtils page = attrGroupService.queryPage(params);

        PageUtils page = attrGroupService.queryPageByCatId(params,catId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getByIdAndCatelongPath(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
